//
// ConfigModel
//
// Copyright (C) jextra.net.
//
//  This file is part of the Crack build system.
//
//  The Crack build system is free software; you can redistribute it 
//  and/or modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//
//  The Crack build system is distributed in the hope that it will be 
//  useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with the Crack build system; if not, write to the Free
//  Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
//  02111-1307 USA.
//

package net.jextra.crackupdaterweb;

import java.io.*;
import java.util.*;
import net.jextra.crack.updater.*;

public class ConfigModel
{
    // ============================================================
    // Fields
    // ============================================================

    public static final String PROP_CRACKUPDATER_ROOT_DIR = "crackupdater.root.dir";

    private File _updaterRootDir;

    private FileUpdater _updater;

    // ============================================================
    // Constructors
    // ============================================================

    public ConfigModel()
        throws IOException
    {
        read();
    }

    // ============================================================
    // Methods
    // ============================================================

    // ----------
    // public
    // ----------

    public void read()
        throws IOException
    {
        _updaterRootDir = null;
        _updater = null;

        File propsFile = getPropsFile();
        if ( !propsFile.exists() )
        {
            return;
        }
        Properties props = new Properties();
        FileReader in = new FileReader( propsFile );
        props.load( in );
        in.close();

        String updaterString = props.getProperty( PROP_CRACKUPDATER_ROOT_DIR );
        if ( updaterString != null )
        {
            File dir = new File( updaterString );
            if ( dir.exists() && dir.isDirectory() )
            {
                _updaterRootDir = dir;
                _updater = new FileUpdater( _updaterRootDir );
            }
        }
    }

    public void save()
        throws IOException
    {
        File propsFile = getPropsFile();
        Properties props = new Properties();
        
        if ( _updaterRootDir != null )
        {
            props.setProperty( PROP_CRACKUPDATER_ROOT_DIR, _updaterRootDir.getAbsolutePath() );
        }

        FileWriter out = new FileWriter( propsFile );
        props.store( out, "" );
        out.close();

    }

    public File getUpdaterRootDir()
        throws IOException
    {
        return _updaterRootDir;
    }

    public void setUpdaterRootDir( File dir )
    {
        _updaterRootDir = dir;
    }

    public boolean isUpdaterRootDirValid()
        throws IOException
    {
        File dir = getUpdaterRootDir();
        if ( dir == null )
        {
            return true;
        }

        return dir.exists();
    }

    public FileUpdater getUpdater()
    {
        return _updater;
    }

    // ----------
    // private
    // ----------

    private File getPropsFile()
        throws IOException
    {
        String catHome = System.getProperty( "catalina.home" );
        File confDir = new File( catHome, "conf" );
        confDir.mkdirs();
        File propsFile = new File( confDir, "crackupdaterweb.properties" );

        return propsFile;
    }
}

